from fastapi import HTTPException

from src.region_service.schemas import Region_schema

from src.database.models import (
    Weather,
    Weather_to_forecast,
    Forecast,
    Region
)
from src.weather_service.schemas_weather import (
    Weather_to_forecast_schema,
    Weather_response_with_region_name,
    Weather_record
)

from src.database.my_crud import My_crud

from src.user_service.service.auth import raise_401_error

weather_crud = My_crud(Weather)
forecast_crud = My_crud(Forecast)
weather_to_forecast_crud = My_crud(Weather_to_forecast)
region_crud = My_crud(Region)


class Region_weather_service:

    async def change_region_id(
        self,
        regionId,
        weatherId,
        user_id
    ):
        await raise_401_error(user_id)

        if not await region_crud.exist([Region.id == regionId]):
            raise HTTPException(status_code=404, detail="bad regionId")

        if not await weather_crud.exist([Weather.id == weatherId]):
            raise HTTPException(status_code=404, detail="bad weatherId")

        weather_record = await weather_crud.patch(
            filters=[Weather.id == weatherId],
            new_data=dict(regionId=regionId),
            Output_model=Weather_record
        )

        relations = await weather_to_forecast_crud.get(
            filters=[Weather_to_forecast.weatherId == weatherId],
            Output_model=Weather_to_forecast_schema,
            multi=True
        )

        weatherForecasts = [relation.weatherForecast for relation in relations]

        regionName = (await region_crud.get(
            filters=[Region.id == weather_record.regionId],
            Output_model=Region_schema
        )).name

        result = Weather_response_with_region_name(
            regionName=regionName,
            weatherForecast=weatherForecasts,
            **weather_record.model_dump()
        )
        result.regionId = regionId

        return result

    async def delete_weather_for_region(
        self,
        regionId: int,
        weatherId: int,
        user_id: int
    ):
        await raise_401_error(user_id)

        if not await region_crud.exist([Region.id == regionId]):
            raise HTTPException(status_code=404, detail="bad regionId")

        relations = await weather_to_forecast_crud.get(
            filters=[Weather_to_forecast.weatherId == weatherId],
            Output_model=Weather_to_forecast_schema,
            multi=True
        )

        weatherForecasts = [relation.weatherForecast for relation in relations]

        for weatherForecast in weatherForecasts:
            forecast_crud.remove([Forecast.id == weatherForecast])

        weather_crud.remove(Weather.id == weatherId)

        region_record = await region_crud.get(
            filters=[Region.id == regionId],
            Output_model=Region_schema
        )
        region_record = region_record.model_dump()
        # region_record.pop("accountId")
        region_record.pop("regionType")
        return region_record
